const fs = require('fs');
const path = require('path');

// 源目录和目标目录
const sourceDir = path.join(__dirname, '../artifacts/contracts');
const targetDir = path.join(__dirname, '../../data-hunter-front/src/interact');

// 确保目标目录存在
if (!fs.existsSync(targetDir)) {
    fs.mkdirSync(targetDir, { recursive: true });
}

// 复制 ABI 文件
function copyAbi(contractName, contractAddress) {
    const sourceFile = path.join(sourceDir, `${contractName}.sol/${contractName}.json`);
    const targetFile = path.join(targetDir, `${contractName}.json`);
    
    try {
        const artifact = require(sourceFile);
        // 保存 ABI 和地址
        fs.writeFileSync(
            targetFile,
            JSON.stringify({
                abi: artifact.abi,
                address: contractAddress
            }, null, 2)
        );
        console.log(`✅ Copied ${contractName} ABI and address to ${targetFile}`);
    } catch (error) {
        console.error(`❌ Error copying ${contractName} ABI:`, error.message);
    }
}

// 导出函数
module.exports = copyAbi;
